package com.gzgs.service.impl;

import com.gzgs.dao.RoleDao;
import com.gzgs.domain.Permission;
import com.gzgs.domain.Role;
import com.gzgs.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Role> findAll() throws Exception {
        return roleDao.findAll();
    }

    @Override
    public void saveRole(Role role) throws Exception {
        roleDao.saveRole(role);
    }

    @Override
    public Role findById(String id) throws Exception {
        return roleDao.findById(id);
    }

    @Override
    public void deleteRole(String id) throws Exception {
        roleDao.deleteFromRole_Permission(id);
        roleDao.deleteFromUser_Role(id);
        roleDao.deleteRole(id);
    }

    @Override
    public List<Permission> findOtherPermissions(String roleId) throws Exception {
        return roleDao.findOtherPermissions(roleId);
    }

    @Override
    public void addPermissionToRole(String roleId, String[] permissionIds) throws Exception {
        for(String permissionId:permissionIds){
            roleDao.addPermissionToRole(roleId,permissionId);
        }
    }
}
